package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.Address;
import domain.Contact;
import domain.ContactGroup;
import domain.DAOAddress;
import domain.DAOContact;
import domain.DAOContactGroup;
import domain.DAOFactory;
import domain.DAOPhoneNumber;
import domain.PhoneNumber;


/**
 * Servlet implementation class AddContactServlet
 */

public class AddContactServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddContactServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		DAOContact daoC=DAOFactory.getDAOContact();		
		DAOAddress daoA=DAOFactory.getDAOAddress();
		DAOContactGroup daoG=DAOFactory.getDAOContactGroup();		
		DAOPhoneNumber daoP=DAOFactory.getDAOPhoneNumber();	
		
	
		String firstName=request.getParameter("firstname");
		String lastName=request.getParameter("lastname");
		String email=request.getParameter("email");
		Contact contact=daoC.addContact(firstName, lastName, email);	
		
		/***********************Address*********************************/
		String street=request.getParameter("street");
		String city=request.getParameter("city");
		String zip=request.getParameter("zip");
		String country=request.getParameter("country");
		Address address=daoA.addAddress(street, city, zip, country);
		if(contact==null)	System.out.println("Contact est null");
		contact.setAddress(address);
		
		/***********************Phone Number*********************************/
		String mobile=request.getParameter("mobile");
		String home=request.getParameter("home");
		String office=request.getParameter("office");
		if(!mobile.equals("")){
			PhoneNumber ph=daoP.addPhoneNumber("mobile", mobile);
			contact.getPhones().add(ph);
			ph.setContact(contact);
		}
		if(!home.equals("")){
			PhoneNumber ph=daoP.addPhoneNumber("home", home);
			contact.getPhones().add(ph);
			ph.setContact(contact);
		}
		if(!office.equals("")){
			PhoneNumber ph=daoP.addPhoneNumber("office", office);
			contact.getPhones().add(ph);
			ph.setContact(contact);
		}
		
		/***********************Group Name*********************************/
		String groupName=request.getParameter("group");
		ContactGroup contactGroup=daoG.addGroupName(groupName);
		contact.getBooks().add(contactGroup);
		contactGroup.getContacts().add(contact);
		
		
		//appel des methodes de fermeture de sessions dans tous les DAO
			
		getServletContext().getRequestDispatcher("/index.html").forward(request, response);
	}

}
